//
//  zidingyiViewController.m
//  HAODE
//
//  Created by panquanReabam.com on 15/11/10.
//  Copyright © 2015年 reabam.com. All rights reserved.
//

#import "zidingyiViewController.h"

#import "HAODEsingleton.h"
#import "UIViewExt.h"
#import "HttpRequest.h"
#import "JSONKit.h"
#import "addhuiyuanViewController.h"

#import "RDVTabBarController.h"
#import "MJRefresh.h"
#import "UIButton+WebCache.h"
#import "huiyuanDetailsViewController.h"
#import "GetProductListViewController.h"
#import "TheLoginViewController.h"
#import "shouyingViewController.h"
#import "SalesAnalysisDetailPageViewController.h"
@interface zidingyiViewController ()<MBProgressHUDDelegate,UIGestureRecognizerDelegate,UINavigationControllerDelegate,UITextFieldDelegate,UISearchBarDelegate,UIWebViewDelegate,SalesAnalysisDetailPageViewControllerChild>
@property (strong, nonatomic) NSString *areaValue;
@property (strong, nonatomic) NSString*cityValue;
@property(nonatomic,retain)UIDatePicker* da;

@end

@implementation zidingyiViewController

{
    
    HAODEsingleton* commdity;
    UITextField*endDateTextFied;
    UITextField*openTimeTextFied;
    UIView*view1;
    UIView*view;
    UIWebView*webview;
    double time1;
    double time2;
}

@synthesize alt = alt;
-(void)BackAction:(UIButton *)sender{
    
    [self.navigationController popViewControllerAnimated:YES];
    
    
    
}
- (void)viewDidDisappear:(BOOL)animated

{
    
    [super viewDidDisappear:animated];
    
    
    
    self.navigationController.delegate = nil;
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [[self rdv_tabBarController] setTabBarHidden:YES];
    
        self.navigationController.navigationBar.barStyle = UIBaselineAdjustmentNone;
    UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 43,ScreenWidth, 1)];
    [lineLabel setBackgroundColor:[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1.0]];
    [self.navigationController.navigationBar addSubview:lineLabel];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setBarTintColor:Color];
    
    UIImage* backImage = [UIImage imageNamed:@"fanhui"];
    CGRect backframe = CGRectMake(0,0,30,30);
    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    backButton.titleLabel.textAlignment=NSTextAlignmentLeft;
    [backButton addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    self.navigationController.delegate = self;
    
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    
    
    self.navigationController.navigationBarHidden = NO;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@""] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage =[[UIImage alloc] init];
    [self add];
    
  
    
}
//回收键盘
-(void)OnTapDone{
    
    
    [openTimeTextFied resignFirstResponder];
    [endDateTextFied resignFirstResponder];
    
}
-(void)tiaojiemian:(NSString *)sender{
    
   
    
    [self add];
    
    
    
}
- (void)viewDidLoad
{
    
    [super viewDidLoad];
    
}
-(void)add{
    
    
    [view1 removeFromSuperview];
    
    view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 10)];
    view.backgroundColor=zuiqianhuicolor;
    [self .view addSubview:view];
    UIView* myToolBar=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, viewheight)];
    myToolBar.backgroundColor=jianpancolor;
    UIButton*  tubtnDone=[[UIButton alloc]initWithFrame:CGRectMake(logoX,logoY, logoheight, logowidth)];
    [tubtnDone setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [tubtnDone addTarget:self action:@selector(OnTapDone) forControlEvents:UIControlEventTouchUpInside];
    [myToolBar addSubview:tubtnDone];
    UIButton*btnDone111=[[UIButton alloc]initWithFrame:CGRectMake(myToolBar.frame.size.width - 55, 12, 40, 16)];
    [btnDone111 setTitle:@"确定" forState:UIControlStateNormal];
    [btnDone111 setTitleColor:jianpancolor1 forState:UIControlStateNormal];
    btnDone111 .titleLabel .font=[UIFont systemFontOfSize:daxiao];
    [btnDone111 addTarget:self action:@selector(OnTapDone) forControlEvents:UIControlEventTouchUpInside];
    [myToolBar addSubview:btnDone111];
    myToolBar.alpha=alp;
    UIButton* dabtn=[[UIButton alloc]initWithFrame:CGRectMake(myToolBar.frame.size.width-60, 0, 60, 40)];
    [dabtn addTarget:self action:@selector(OnTapDone) forControlEvents:UIControlEventTouchUpInside];
    [myToolBar addSubview:dabtn];

    _da = [[UIDatePicker alloc] init];
    [_da addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged];
    _da.datePickerMode = UIDatePickerModeDate;
    _da.backgroundColor=[UIColor whiteColor];

    view1=[[UIView alloc]initWithFrame:CGRectMake(0, 10, ScreenWidth, ScreenHeight)];
    view1.backgroundColor=[UIColor whiteColor];
   [self .view addSubview:view1];
    UILabel* beginDate=[[UILabel alloc]initWithFrame:CGRectMake(20, 0,70,45)];
    beginDate.text=[NSString stringWithFormat:@"%@", @"开始时间"];
    beginDate.font=[UIFont systemFontOfSize:contenfot];
    beginDate.textColor=[UIColor blackColor];
    [view1 addSubview:beginDate];
    UIButton*jiantouBtn1= [[UIButton alloc]init];
    jiantouBtn1.frame = CGRectMake(ScreenWidth-30,0+15,8, 13);
    [jiantouBtn1 setImage:[UIImage imageNamed:@"xiangzuo-.png"] forState:UIControlStateNormal];
    [view1 addSubview:jiantouBtn1];
    
    openTimeTextFied=[[UITextField alloc]initWithFrame:CGRectMake(beginDate.right,  0, ScreenWidth*530/750,  45)];
    openTimeTextFied.borderStyle = UITextBorderStyleNone;
    openTimeTextFied.font = [UIFont systemFontOfSize: contenfot];
    openTimeTextFied.textColor=[UIColor blackColor];
    openTimeTextFied.autocapitalizationType = UITextAutocapitalizationTypeSentences;
    openTimeTextFied.tag=5;
    openTimeTextFied.placeholder = @"请选择开始日期"; //默认显示的字
    openTimeTextFied.delegate = self;
    [openTimeTextFied setUserInteractionEnabled:YES];
    openTimeTextFied.clearButtonMode = UITextFieldViewModeWhileEditing;
    [view1 addSubview:openTimeTextFied];
    
    UILabel* endDate=[[UILabel alloc]initWithFrame:CGRectMake(20, 45,70,45)];
    endDate.text=[NSString stringWithFormat:@"%@", @"结束时间"];
    endDate.font=[UIFont systemFontOfSize:contenfot];
    endDate.textColor=[UIColor blackColor];
    [view1 addSubview:endDate];
    UIButton*jiantouBtn= [[UIButton alloc]init];
    jiantouBtn.frame = CGRectMake(ScreenWidth-30,45+15,8, 13);
    [jiantouBtn setImage:[UIImage imageNamed:@"xiangzuo-.png"] forState:UIControlStateNormal];
    [view1 addSubview:jiantouBtn];
    
    
    
    endDateTextFied=[[UITextField alloc]initWithFrame:CGRectMake(endDate.right,  45, ScreenWidth*530/750,  45)];
    endDateTextFied.borderStyle = UITextBorderStyleNone;
    endDateTextFied.font = [UIFont systemFontOfSize: contenfot];
    endDateTextFied.textColor=[UIColor blackColor];
    endDateTextFied.autocapitalizationType = UITextAutocapitalizationTypeSentences;
    endDateTextFied.tag=6;
    endDateTextFied.placeholder = @"请选择结束日期"; //默认显示的字
    
    endDateTextFied.delegate = self;
    [endDateTextFied setUserInteractionEnabled:YES];
    endDateTextFied.clearButtonMode = UITextFieldViewModeWhileEditing;
    [view1 addSubview:endDateTextFied];
    [_da setAccessibilityLanguage:@"Chinese"];
    
    openTimeTextFied.inputView = _da;
    endDateTextFied.inputView = _da;
    openTimeTextFied.inputAccessoryView = myToolBar;
   
    endDateTextFied.inputAccessoryView = myToolBar;
    
    
    
    UIButton*login = [[UIButton alloc] initWithFrame:CGRectMake(10, 150, ScreenWidth-20, anniuHeigth)];
    [login setTitle:@"确认" forState:UIControlStateNormal];
    [login setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    login.backgroundColor=luseColor;
    [login.layer setCornerRadius:3.0]; //设置矩圆角半径
    [login.layer setBorderWidth:0.3];   //边框宽度
    CGColorSpaceRef tureBTnSpace = CGColorSpaceCreateDeviceRGB();
    CGColorRef tureBTncolorref = CGColorCreate(tureBTnSpace,(CGFloat[]){23/255.0, 148/255.0, 152/255.0, 1.0});
    [login.layer setBorderColor:tureBTncolorref];//边框颜色
    [login addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
    [view1 addSubview:login];

    
    
    
    
    
}
- (void)dateChanged:(id)sender
{
    
    UIDatePicker* picker = (UIDatePicker*)sender;
    NSDateFormatter* df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"yyyy-MM-dd"];
    
    
    
    
    
    
    
    
    if ([openTimeTextFied isFirstResponder])
    {
        openTimeTextFied.text = [NSString stringWithFormat:@"%@", [df stringFromDate:picker.date]];
        
        
        NSString* string2 = openTimeTextFied.text;
        NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
        [dateFormatter2 setDateFormat: @"yyyy-MM-dd "];
        NSDate *destDate2= [dateFormatter2 dateFromString:string2];
        NSTimeInterval  timeInterval1 = [destDate2 timeIntervalSinceDate:[NSDate date]];
        time1=timeInterval1;
        
        

    }else{
        
        
        
        endDateTextFied.text = [NSString stringWithFormat:@"%@", [df stringFromDate:picker.date]];
        
        NSString* string2 = endDateTextFied.text;
        NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
        [dateFormatter2 setDateFormat: @"yyyy-MM-dd "];
        NSDate *destDate2= [dateFormatter2 dateFromString:string2];
        NSTimeInterval  timeInterval2 = [destDate2 timeIntervalSinceDate:[NSDate date]];
        
        time2=timeInterval2;
        if (time2<time1 ) {
            endDateTextFied.text=nil;
          [self showAlertMessage:@"结束日期必须大于开始日期"];
        }
        
        
    }
    
    
    
    
    
    
    
    
}

    
-(void)login{
    
    if ([openTimeTextFied.text length] <1)
    {
        [self showAlertMessage:@"请选择开始日期"];
    }
    else{
        
        
        if ([endDateTextFied.text length] < 1)
        {
            [self showAlertMessage:@"请选择结束日期"];
        }
        else
        {

    
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"tokenId"];
    if (Token ==nil) {
        Token=@"dd";
    }
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setValue:Token forKey:@"tokenId"];
    [param setValue:openTimeTextFied.text forKey:@"beginDate"];
    [param setValue:endDateTextFied.text forKey:@"endDate"];
    
    [HttpRequest requestData:@"UserCenter/SalesAnalysis/DetailPage" params:param success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            
            NSLog(@"33333333====%@",severDataDict.dataDict);
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [Global hideProgressHUDInternal:self.view];
                [view1 removeFromSuperview];
                
                webview  = [[UIWebView   alloc]  initWithFrame:CGRectMake( 0,  0,  ScreenWidth ,  ScreenHeight-64-50)];
                webview.scalesPageToFit = TRUE;
                [webview   setUserInteractionEnabled: YES ];	 //是否支持交互
                [webview   setDelegate: self ];				 //委托
                [webview   setOpaque: YES ];					 //透明
                [self.view  addSubview : webview];
                NSString * path =[NSString stringWithFormat:@"%@",[severDataDict.dataDict objectForKey:@"page"]];
                
                [webview loadRequest:[NSURLRequest requestWithURL:[[NSURL alloc] initWithString :path]]];
                
                
                
            }else{
                [Global hideProgressHUDInternal:self.view];
                NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                // [Global showTextHud:requestResultStr view:self.view];
                [Global showTextHudSub:requestResultStr view:self.view];
                
                
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==9999) {
                    
                    NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                    [Global showTextHud:requestResultStr view:self.view];
                    
                    
                    TheLoginViewController * second =
                    [[TheLoginViewController alloc]
                     initWithNibName:@"TheLoginViewController"
                     bundle:nil];
                    //        second.textt =self.text.text;
                    //        //    second.label.text = @"正向";
                    [self presentViewController:second animated:YES completion:nil];
                    
                }
            }
        }
        else
        {
            [Global hideProgressHUDInternal:self.view];
            
            [Global showTextHudSub:@"连接失败" view:self.view];
        }
        
    }];
        }}
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)showAlertWait
{
    alt = [[UIAlertView alloc] initWithTitle:@"网络不稳定 ！" message:@"请确认网络" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
    
}

- (void)showAlertMessage:(NSString *)msg
{
    alt = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
}
- (void)hideAlert
{
    if (alt != nil)
    {
        [alt dismissWithClickedButtonIndex:0 animated:YES];
        alt = nil;
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
